<?php
namespace Manage\Controller;
use Think\Controller;

/**
 * Class AdminMobileAdminController
 * @package Manage\Controller
 * PC后台管理移动端人员
 */
class AdminMobileAdminController extends Controller {

    private $model_name = 'MobileAdmin';
    private $record_model = 'MobileRegest';

    public function index(){
        $this->display();
    }

    public function get_all(){
        $list = get_date_grid($this->model_name);
        echo json_encode($list);
    }

    public function add(){
        $model = D($this->model_name);
        if(IS_POST){
            $data = filterInput($_POST);
            $model->addObj($data, 'id');
            $this->success(LANG('save_success'));
            return;
        }else if(IS_GET){

        }
        $this->display();
    }

    public function edit(){
        $model = D($this->model_name);
        if(IS_POST){
            $post_data = filterInput($_POST);
            $model->save($post_data);
            $this->success(LANG('save_success'));
            return;
        }else if(IS_GET){
            $data = filterInput($_GET);
            $item = $model->queryObj($data['id'], 'id');

            $this->assign('item', $item);
        }
        $this->display();
    }

    public function deleteArray(){
        $data = filterInput($_POST);
        $ids = $data['ids'];
        $model = D($this->model_name);
        $result = $model->deleteArray($ids);
        if($result){
            echo 1;
        }else{
            echo 0;
        }
    }

    public function show_mobile_admin(){
        $this->display();
    }

    public function get_tree_json(){
        $model = D($this->model_name);
        $ids = I('get.ids')?I('get.ids'):'';
        $list = $model->get_tree_show_json($ids);
        echo json_encode($list);
    }

    public function show_reg_log(){
        $admin_model = D('MobileAdmin');
        $this->assign('admins' , $admin_model->getAll());
        $this->display();
    }

    public function get_all_reg_log(){
        $list = get_date_grid($this->record_model);
        echo json_encode($list);
    }

    public function show_on_map(){
        header("Content-type: text/html; charset=utf-8");
        if(!I('get.gps')||I('get.gps')=='0,0') {
            $gps = '102.70281,25.033455';
            echo '由于gps信号弱，没有获取到坐标';
            exit;
        }
        else
            $gps = I('get.gps');
        $person = D($this->model_name)->queryObj(I('get.uid'), 'id');
        $record = D('MobileRegest')->queryObj(I('get.id'), 'id');
        $this->assign('person', $person);
        $this->assign('record', $record);
        $this->assign('gps', $gps);
        $this->display('map');
    }

    public function export_record(){
        header("Content-type: text/html; charset=utf-8");
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $art = array(
            'station' => 'PumpingStation',
            'pid_field' => 'uid',
            'record_pid_field' => 'uid',
            'sta_name_field' => 'ps_name',
        );
        $station_type = I('get.station')?I('get.station'):'';
        $pids = I('get.pid')?I('get.pid'):0;
        $start = I('get.start')?I('get.start').' 00:00:00':'2016-01-01 00:00:00';
        $end = I('get.end')?I('get.end').' 23:59:59':'2016-12-31 00:00:00';
        $str = ' createtime>=\''.$start.'\' and createtime<=\''.$end.'\'';
        $record_model = D($this->record_model);
        $temp_file = './excelfile/reglog.xls';
        $out_file_name = '交接班记录';
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($temp_file);
        if(!$php_reader->canRead($filename)){
            echo '无法识别的Excel文件！';
            return false;
        }
        //先查找出该泵站的泵机，循环遍历泵机的记录，重复写入
        if($station_type == 'station'){
            $station_model = D('PumpingStation');
            $art['sta_name_field'] = 'ps_name';

        }else if($station_type == 'csdt'){
            $station_model = D('Csdt');
            $art['sta_name_field'] = 'ps_name';
        }else{
            $station_model = D('Gate');
            $art['sta_name_field'] = 'gate_name';
        }
        $colum_array = array(
            1=>'C',22=>'X',
            2=>'D',23=>'Y',
            3=>'E',24=>'Z',
            4=>'F',25=>'AA',
            5=>'G',26=>'AB',
            6=>'H',27=>'AC',
            7=>'I',28=>'AD',
            8=>'J',29=>'AE',
            9=>'K',30=>'AF',
            10=>'L',31=>'AG',
            11=>'M',32=>'AH',
            12=>'N',
            13=>'O',
            14=>'P',
            15=>'Q',
            16=>'R',
            17=>'S',
            18=>'T',
            19=>'U',
            20=>'V',
            21=>'W',
        );
        if(!S('mobile_admin')){
            $m = D('MobileAdmin');
            $s = $m->select();
            $count = count($s);
            $a = array();
            for($i=0;$i<$count;$i++){
                $a[$s[$i]['id']] = $s[$i];
            }
            S('mobile_admin', json_encode($a));
        }
        $mobile_admins = json_decode(S('mobile_admin'), true);
        $php_excel = $php_reader->load($filename);
        $index = 0;
        $pids = explode(',', $pids);
        foreach($pids as $pid){
            $start_row = 6;
            $row = $start_row;
            $station = $station_model->queryObj($pid, 'id');
            if($station_type == 'station'){
                $opr = $station['ps_opr'];
            }else if($station_type == 'csdt'){
                $opr = $station['ps_opr'];
            }else{
                $opr = $station['gate_opr'];
            }
            $new_sheet = new \PHPExcel_Worksheet($php_excel); //创建一个工作表
            $php_excel->addSheet($new_sheet); //插入工作表
            $php_excel->setActiveSheetIndex($index);
            $currentSheet = $this->set_excel_header($php_excel->getActiveSheet($index), $colum_array);
            $currentSheet->setTitle($station['ps_name']);
            $ids = array_filter(explode(',', $opr));
            $currentSheet->setCellValue('B3',$station[$art['sta_name_field']]);
            $currentSheet->setCellValue('L3',substr($start,0,7));

            foreach($ids as $id){
                //设定名字
                $currentSheet->mergeCells('A'.$row.':'.'A'.($row+1));
                $currentSheet->setCellValue('A'.$row,$mobile_admins[$id]['realname']);
                $currentSheet->setCellValue('B'.$row,'上班 ');
                $currentSheet->setCellValue('B'.($row+1),'下班');
                $map[$art['record_pid_field']] = array('eq', $id);
                $list = $record_model->where($map)->where($str)->order('createtime asc')->select();
                $list = $record_model->bz_record($id, $list);
                foreach($list as $k => $record){
                    $colum = $colum_array[intval(substr($record['createtime'],8,2))];
                    if($record['status']==1||$record['status']==-1){
                        $currentSheet->setCellValue($colum.$row,substr($record['createtime'],11,5));
                    }else{
                        $currentSheet->setCellValue($colum.($row+1),substr($record['createtime'],11,5));
                    }
                }
                $row+=2;
            }
            $currentSheet->getStyle('A'.$start_row.':AH'.$row)->getFont()->setName('宋体');
            $currentSheet->getStyle('A'.$start_row.':AH'.$row)->getFont()->setSize(11);
            $currentSheet->getStyle('A'.$start_row.':AH'.$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $currentSheet->getStyle('A'.$start_row.':AH'.$row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $currentSheet->getStyle('A1:AH'.($row-1))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $currentSheet->getColumnDimension('D')->setWidth(8);
            $currentSheet->getColumnDimension('E')->setWidth(8);
            $currentSheet->getColumnDimension('F')->setWidth(8);
            $currentSheet->getColumnDimension('G')->setWidth(8);
            $currentSheet->getColumnDimension('H')->setWidth(8);
            $currentSheet->getColumnDimension('I')->setWidth(8);
            $currentSheet->getColumnDimension('J')->setWidth(8);
            $currentSheet->getColumnDimension('K')->setWidth(8);
            $currentSheet->getColumnDimension('L')->setWidth(8);
            $currentSheet->getColumnDimension('M')->setWidth(8);
            $currentSheet->getColumnDimension('N')->setWidth(8);
            $currentSheet->getColumnDimension('O')->setWidth(8);
            $currentSheet->getColumnDimension('P')->setWidth(8);
            $currentSheet->getColumnDimension('Q')->setWidth(8);
            $currentSheet->getColumnDimension('R')->setWidth(8);
            $currentSheet->getColumnDimension('S')->setWidth(8);
            $currentSheet->getColumnDimension('T')->setWidth(8);
            $currentSheet->getColumnDimension('U')->setWidth(8);
            $currentSheet->getColumnDimension('V')->setWidth(8);
            $currentSheet->getColumnDimension('W')->setWidth(8);
            $currentSheet->getColumnDimension('X')->setWidth(8);
            $currentSheet->getColumnDimension('Y')->setWidth(8);
            $currentSheet->getColumnDimension('Z')->setWidth(8);
            $currentSheet->getColumnDimension('AA')->setWidth(8);
            $currentSheet->getColumnDimension('AB')->setWidth(8);
            $currentSheet->getColumnDimension('AC')->setWidth(8);
            $currentSheet->getColumnDimension('AD')->setWidth(8);
            $currentSheet->getColumnDimension('AE')->setWidth(8);
            $currentSheet->getColumnDimension('AF')->setWidth(8);
            $currentSheet->getColumnDimension('AG')->setWidth(8);
            $index++;
        }
        $php_writer = new \PHPExcel_Writer_Excel5($php_excel);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        $out = substr($start,0,10).'/'.substr($end,0,10).'_'.$station[$art['sta_name_field']].$out_file_name;
        header("Content-Disposition: attachment;filename=".$out.".xls");
        header('Cache-Control: max-age=0');
        $php_writer->save('php://output');
    }

    private function set_excel_header($sheet, $cloum){
        $sheet->mergeCells('A1:AH2');
        $sheet->setCellValue('A1',"交  接  班  时  间  记  录  表");
        $sheet->getRowDimension(1)->setRowHeight(35);
        $sheet->getStyle('A1')->getFont()->setName('宋体');
        $sheet->getStyle('A1')->getFont()->setSize(24);
        $sheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $sheet->setCellValue('A3',"   单位:  ");
        $sheet->mergeCells('B3:G3');
        $sheet->mergeCells('H3:K3');
        $sheet->setCellValue('H3',"统计时间");
        $sheet->mergeCells('L3:AH3');
        $sheet->mergeCells('A4:A5');
        $sheet->setCellValue('A4',"姓名");
        $sheet->mergeCells('B4:B5');
        $sheet->setCellValue('B4',"上、下班时间");
        for($i=1;$i<=31;$i++){
            $sheet->mergeCells($cloum[$i].'4:'.$cloum[$i].'5');
            $sheet->setCellValue($cloum[$i].'4',$i);
        }
        $sheet->mergeCells('AH4:AH5');
        $sheet->setCellValue('AH4',"备注");
        $sheet->getStyle('A4:AH5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('A4:AH5')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        return $sheet;
    }

    public function export_record_all(){
        header("Content-type: text/html; charset=utf-8");
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Reader.Excel5");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $art = array(
            'station' => 'PumpingStation',
            'pid_field' => 'uid',
            'record_pid_field' => 'uid',
            'sta_name_field' => 'ps_name',
        );
        $station_type = I('get.station')?I('get.station'):'';
        $start = I('get.start')?I('get.start').' 00:00:00':'2016-01-01 00:00:00';
        $end = I('get.end')?I('get.end').' 23:59:59':'2016-12-31 00:00:00';
        $str = ' createtime>=\''.$start.'\' and createtime<=\''.$end.'\'';
        $record_model = D($this->record_model);
        $temp_file = './excelfile/reglog.xls';
        $out_file_name = '交接班记录';
        $php_reader = new \PHPExcel_Reader_Excel5();
        $filename = file_zh_cn_icvon($temp_file);
        if(!$php_reader->canRead($filename)){
            echo '无法识别的Excel文件！';
            return false;
        }
        //先查找出该泵站的泵机，循环遍历泵机的记录，重复写入
        if($station_type == 'station'){
            $station_model = D('PumpingStation');
            $art['sta_name_field'] = 'ps_name';
            if(!S('station_all')){
                S('station_all', json_encode($station_model->field('id,ps_name,ps_opr')->select()));
            }
            $pids = json_decode(S('station_all'), true);
        }else if($station_type == 'csdt'){
            $station_model = D('Csdt');
            $art['sta_name_field'] = 'ps_name';
            if(!S('csdt_all')){
                S('csdt_all', json_encode($station_model->field('id,ps_name,ps_opr')->select()));
            }
            $pids = json_decode(S('csdt_all'), true);
        }
        $colum_array = array(
            22=>'X',
            2=>'D',23=>'Y',
            3=>'E',24=>'Z',
            4=>'F',25=>'AA',
            5=>'G',26=>'AB',
            6=>'H',27=>'AC',
            7=>'I',28=>'AD',
            8=>'J',29=>'AE',
            9=>'K',30=>'AF',
            10=>'L',31=>'AG',
            11=>'M',32=>'AH',
            12=>'N',33=>'AI',
            13=>'O',
            14=>'P',
            15=>'Q',
            16=>'R',
            17=>'S',
            18=>'T',
            19=>'U',
            20=>'V',
            21=>'W',
        );
        if(!S('mobile_admin')){
            $m = D('MobileAdmin');
            $s = $m->select();
            $count = count($s);
            $a = array();
            for($i=0;$i<$count;$i++){
                $a[$s[$i]['id']] = $s[$i];
            }
            S('mobile_admin', json_encode($a));
        }
        $mobile_admins = json_decode(S('mobile_admin'), true);
        $php_excel = $php_reader->load($filename);
        $new_sheet = new \PHPExcel_Worksheet($php_excel); //创建一个工作表
        $php_excel->addSheet($new_sheet); //插入工作表
        $php_excel->setActiveSheetIndex(0);
        $currentSheet = $this->set_excel_header_all($php_excel->getActiveSheet(0), $colum_array);
        $currentSheet->setTitle('考勤汇总表');
        $currentSheet->setCellValue('M3',substr($start,0,7));
        $index = 0;
        $start_row = 6;
        $row = $start_row;
        foreach($pids as $station){
            $ids = array_filter(explode(',', $station['ps_opr']));
            if(empty($ids)){
                continue;
            }
            $start_fys = $row;
            foreach($ids as $id){
                //设定名字
                $currentSheet->mergeCells('B'.$row.':'.'B'.($row+1));
                $currentSheet->setCellValue('B'.$row,$mobile_admins[$id]['realname']);
                $currentSheet->setCellValue('C'.$row,'上班 ');
                $currentSheet->setCellValue('C'.($row+1),'下班');
                $map[$art['record_pid_field']] = array('eq', $id);
                $list = $record_model->where($map)->where($str)->order('createtime asc')->select();
                foreach($list as $k => $record){
                    $colum = $colum_array[intval(substr($record['createtime'],8,2))+1];
                    if($record['status']==1||$record['status']==-1){
                        $currentSheet->setCellValue($colum.$row,substr($record['createtime'],11,5));
                    }else{
                        $currentSheet->setCellValue($colum.($row+1),substr($record['createtime'],11,5));
                    }
                }
                $row+=2;
            }
            //设定设施名称
            $currentSheet->mergeCells('A'.$start_fys.':'.'A'.($row-1));
            $currentSheet->setCellValue('A'.$start_fys,$station['ps_name']);
            $currentSheet->getStyle('A'.$start_row.':AI'.$row)->getFont()->setName('宋体');
            $currentSheet->getStyle('A'.$start_row.':AI'.$row)->getFont()->setSize(11);
            $currentSheet->getStyle('A'.$start_row.':AI'.$row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $currentSheet->getStyle('A'.$start_row.':AI'.$row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $currentSheet->getStyle('A1:AI'.($row-1))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $currentSheet->getColumnDimension('D')->setWidth(8);
            $currentSheet->getColumnDimension('E')->setWidth(8);
            $currentSheet->getColumnDimension('F')->setWidth(8);
            $currentSheet->getColumnDimension('G')->setWidth(8);
            $currentSheet->getColumnDimension('H')->setWidth(8);
            $currentSheet->getColumnDimension('I')->setWidth(8);
            $currentSheet->getColumnDimension('J')->setWidth(8);
            $currentSheet->getColumnDimension('K')->setWidth(8);
            $currentSheet->getColumnDimension('L')->setWidth(8);
            $currentSheet->getColumnDimension('M')->setWidth(8);
            $currentSheet->getColumnDimension('N')->setWidth(8);
            $currentSheet->getColumnDimension('O')->setWidth(8);
            $currentSheet->getColumnDimension('P')->setWidth(8);
            $currentSheet->getColumnDimension('Q')->setWidth(8);
            $currentSheet->getColumnDimension('R')->setWidth(8);
            $currentSheet->getColumnDimension('S')->setWidth(8);
            $currentSheet->getColumnDimension('T')->setWidth(8);
            $currentSheet->getColumnDimension('U')->setWidth(8);
            $currentSheet->getColumnDimension('V')->setWidth(8);
            $currentSheet->getColumnDimension('W')->setWidth(8);
            $currentSheet->getColumnDimension('X')->setWidth(8);
            $currentSheet->getColumnDimension('Y')->setWidth(8);
            $currentSheet->getColumnDimension('Z')->setWidth(8);
            $currentSheet->getColumnDimension('AA')->setWidth(8);
            $currentSheet->getColumnDimension('AB')->setWidth(8);
            $currentSheet->getColumnDimension('AC')->setWidth(8);
            $currentSheet->getColumnDimension('AD')->setWidth(8);
            $currentSheet->getColumnDimension('AE')->setWidth(8);
            $currentSheet->getColumnDimension('AF')->setWidth(8);
            $currentSheet->getColumnDimension('AG')->setWidth(8);
            $currentSheet->getColumnDimension('AH')->setWidth(8);
            $index++;
        }
        $php_writer = new \PHPExcel_Writer_Excel5($php_excel);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        $out = substr($start,0,10).'/'.substr($end,0,10).'_'.$station[$art['sta_name_field']].$out_file_name;
        header("Content-Disposition: attachment;filename=".$out.".xls");
        header('Cache-Control: max-age=0');
        $php_writer->save('php://output');
    }

    private function set_excel_header_all($sheet, $cloum){
        $sheet->mergeCells('A1:AH2');
        $sheet->setCellValue('A1',"交  接  班  时  间  记  录  表");
        $sheet->getRowDimension(1)->setRowHeight(35);
        $sheet->getStyle('A1')->getFont()->setName('宋体');
        $sheet->getStyle('A1')->getFont()->setSize(24);
        $sheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $sheet->setCellValue('B3',"   单位:  ");
        $sheet->mergeCells('C3:H3');
        $sheet->mergeCells('I3:L3');
        $sheet->setCellValue('I3',"统计时间");
        $sheet->mergeCells('M3:AI3');
        $sheet->mergeCells('A4:A5');
        $sheet->setCellValue('A4',"设施名称");
        $sheet->mergeCells('B4:B5');
        $sheet->setCellValue('B4',"姓名");
        $sheet->mergeCells('C4:C5');
        $sheet->setCellValue('C4',"上、下班时间");
        for($i=2;$i<=32;$i++){
            $sheet->mergeCells($cloum[$i].'4:'.$cloum[$i].'5');
            $sheet->setCellValue($cloum[$i].'4',$i-1);
        }
        $sheet->mergeCells('AI4:AI5');
        $sheet->setCellValue('AI4',"备注");
        $sheet->getStyle('A4:AI5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('A4:AI5')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        return $sheet;
    }
}